<?php
$loader->requireOnce('includes/Datasource_sql.class.php');
class SurgeryParticipant extends ORDataObject {
	var $surgery_participant_id	=	'';
	var $surgery_id				=	'';
	var $person_id				=	'';
	var $role					=	'';
	
	var $_table					=	'surgery_participant';
	var $_key					=	'surgery_participant_id';
	var	$_internalName			=	'SurgeryParticipant';
	var $_foreignKeyList		=	array(
		'person_id'		=>	'person',
		'surgery_id'	=>	'surgery'
	);
	
	
	const SURPAR_ROLE_SURG		=	'surgeon';
	const SURPAR_ROLE_ANES		=	'anesthesiologist';
	const SURPAR_ROLE_TECH		=	'technicant';
	const SURPAR_ROLE_NURS		=	'nurse';
	const SURPAR_ROLE_PATH		=	'internal_pathologist';
	const SURPAR_ROLE_OBSE		=	'observer';
	const SURPAR_ROLE_INTR		=	'introducer';
	
	
	public function SurgeryParticipant(){
		parent::ORDataObject();
	}
	
	public function setup($id = 0){
		$this->set('id',$id);
		if($id>0)
			$this->populate();
	}
	
	public function populate(){
		parent::populate('surgery_participant_id');
	}
	
	public function setData($surgeryId, $personId, $role){
		
		$data	=	array(	
			'surgery_id'	=>	$surgeryId,
			'person_id'		=>	$personId,
			'role'			=>	$role
		);
		$this->populateArray($data);
	}

	public function getId(){
		return $this->surgery_participant_id;
	}
	
	public function setId($id){
		$this->surgery_participant_id=$id;
	}
	
	public static function getParticipantRoleList(){
		$list = Utils::getEnumArrayKey(EnumerationConstant::SURGERY_PARTICIPANT_ROLE);		
		return $list;				
	}
	
	public function getRole(){
		return $this->role;
	}
	
	public static function getRoleText($role){
		$roleList	=	self::getParticipantRoleList();
		if(!array_key_exists($role, $roleList)){
			return '';
		}
		return $roleList[$role];
	}
	
	public function getParticipantName(){
		$sql = 'select name from '.$this->_foreignKeyList['person_id'].
			' where person_id='.(int)$this->person_id;
		$res = $this->_execute($sql);
		if(!empty($res)){
			return $res->fields['name'];
		}
		return '';
	}
	
	public function getIdsBySurgeryId($surgeryId = 0){
		$ds		= new Datasource_sql();
        $sql	= array(
        	'cols'    	=> 	"surgery_participant_id as id",
        	'from'   	=>	"surgery_participant",
        	'where'		=>	"surgery_id=$surgeryId",
        	'orderby' 	=> 'surgery_participant_id'
        );
        $ds->setup($this->_db,$sql);
        $idArray = $ds->toArray('id');
        return $idArray;
	}
	
	public function deleteParticipants($surgeryId = 0){
		$sql	=	"delete from $this->_table where surgery_id=$surgeryId";
		$this->_execute($sql);
	}
	
	public function deleteById($surgeryParticipantId) {
		$sql	=	"delete from $this->_table where $this->_key=$surgeryParticipantId";
		$this->_execute($sql);
		
	}
	
	public function countRoles($surgeryId = 0) {
		$sql = "SELECT COUNT(role) AS countRole FROM $this->_table WHERE surgery_id = $surgeryId GROUP BY role";
		return $this->_execute($sql);
	}
	
	public function getParticipantsBySurgeryId($surgeryId) {
		$ds		= new Datasource_sql();
        $sql	= array(
        	'cols'    	=> 	"sp.person_id, ev.value as role",
        	'from'   	=>	"surgery_participant sp"
                          . " INNER JOIN enumeration_value ev ON sp.role = ev.key "
                          . " INNER JOIN enumeration_definition ed ON ev.enumeration_id = ed.enumeration_id ",
        	'where'		=>	"surgery_id=$surgeryId"
                          . " AND ed.name = '" . EnumerationConstant::SURGERY_PARTICIPANT_ROLE . "'"        	
        );
        $ds->setup($this->_db,$sql);
        return $ds->toArray('person_id', 'role');
	}
	
    public function getParticipantsDetailInfoBySurgeryId($surgeryId) {
        $ds     = new Datasource_sql();
        $sql    = array(
            'cols'      =>  "sp.person_id, p.name, ev.value as role",
            'from'      =>  "surgery_participant sp INNER JOIN person p ON (sp.person_id=p.person_id)"
                          . " INNER JOIN enumeration_value ev ON sp.role = ev.key "
                          . " INNER JOIN enumeration_definition ed ON ev.enumeration_id = ed.enumeration_id ",
            'where'     =>  "surgery_id='$surgeryId'"
                          . " AND ed.name = '" . EnumerationConstant::SURGERY_PARTICIPANT_ROLE . "'"         
        );
        $ds->setup($this->_db,$sql);
        return $ds->toArray();
    }
	
	public function getPersonId() {
		return $this->person_id;
	}
}
?>